
const { app, ipcMain } = require('electron');
const { createConnection, getServerAddress, setServerAddress } = require('./db');
const { createLoginWindow, createSettingsWindow } = require('./windowManager');
const { handleUserLogin } = require('./auth');

app.whenReady().then(() => {
    createConnection();
    createLoginWindow();
});

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

ipcMain.handle('user-login', async (event, username, password) => {
    return await handleUserLogin(event, username, password);
});

ipcMain.on('log-message', (event, message) => {
    console.log(message);
});

ipcMain.handle('open-settings-window', async (event) => {
    return await createSettingsWindow();
});

// 处理获取服务器地址的请求
ipcMain.handle('get-server-address', async () => {

    return getServerAddress();
});

// 处理保存服务器地址的请求
ipcMain.handle('save-server-address', async (event, serverAddressArg) => {
    console.log(`Saving server address: ${serverAddressArg} `, typeof serverAddressArg);
    return setServerAddress(serverAddressArg);
});